package com.lckd.ss.mbg;

import org.mybatis.generator.api.GeneratedXmlFile;
import org.mybatis.generator.api.IntrospectedTable;
import org.mybatis.generator.api.PluginAdapter;
import org.mybatis.generator.api.dom.xml.Attribute;
import org.mybatis.generator.api.dom.xml.Document;
import org.mybatis.generator.api.dom.xml.TextElement;
import org.mybatis.generator.api.dom.xml.XmlElement;
import org.mybatis.generator.internal.util.StringUtility;
import org.mybatis.generator.internal.util.messages.Messages;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * Created by asiam on 2017/4/7 0007.
 */
public class MyMapperConfigPlugin extends PluginAdapter {
    private List<String> mapperFiles = new ArrayList();

    public MyMapperConfigPlugin() {
    }

    @Override
    public boolean validate(List<String> warnings) {
        boolean valid = true;
        if(!StringUtility.stringHasValue(this.properties.getProperty("targetProject"))) {
            warnings.add(Messages.getString("ValidationError.18", "MapperConfigPlugin", "targetProject"));
            valid = false;
        }

        if(!StringUtility.stringHasValue(this.properties.getProperty("targetPackage"))) {
            warnings.add(Messages.getString("ValidationError.18", "MapperConfigPlugin", "targetPackage"));
            valid = false;
        }

        return valid;
    }

    @Override
    public List<GeneratedXmlFile> contextGenerateAdditionalXmlFiles(IntrospectedTable introspectedTable) {
        Document document = new Document("-//mybatis.org//DTD Config 3.0//EN", "http://mybatis.org/dtd/mybatis-3-config.dtd");
        XmlElement root = new XmlElement("configuration");
        document.setRootElement(root);
        root.addElement(new TextElement("<!--"));
        root.addElement(new TextElement("  This file is generated by MyBatis Generator."));
        root.addElement(new TextElement("  This file is the shell of a Mapper Config file - in many cases you will need to add"));
        root.addElement(new TextElement("    to this file before it is usable by MyBatis."));
        StringBuilder sb = new StringBuilder();
        sb.append("  This file was generated on ");
        sb.append(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
        sb.append('.');
        root.addElement(new TextElement(sb.toString()));
        root.addElement(new TextElement("-->"));
        //不生成mappers标签
//        XmlElement mappers = new XmlElement("mappers");
//        root.addElement(mappers);
//        Iterator gxf = this.mapperFiles.iterator();
//
//        while(gxf.hasNext()) {
//            String answer = (String)gxf.next();
//            XmlElement mapper = new XmlElement("mapper");
//            mapper.addAttribute(new Attribute("resource", answer));
//            mappers.addElement(mapper);
//        }

        XmlElement settings = new XmlElement("settings");

        XmlElement setting = new XmlElement("setting");
        setting.addAttribute(new Attribute("name", "lazyLoadingEnabled"));
        setting.addAttribute(new Attribute("value", "false"));
        settings.addElement(setting);

        setting = new XmlElement("setting");
        setting.addAttribute(new Attribute("name", "mapUnderscoreToCamelCase"));
        setting.addAttribute(new Attribute("value", "true"));
        settings.addElement(setting);

        setting = new XmlElement("setting");
        setting.addAttribute(new Attribute("name", "cacheEnabled"));
        setting.addAttribute(new Attribute("value", "false"));
        settings.addElement(setting);

        setting = new XmlElement("setting");
        setting.addAttribute(new Attribute("name", "useGeneratedKeys"));
        setting.addAttribute(new Attribute("value", "true"));
        settings.addElement(setting);

        setting = new XmlElement("setting");
        setting.addAttribute(new Attribute("name", "defaultExecutorType"));
        setting.addAttribute(new Attribute("value", "REUSE"));
        settings.addElement(setting);

        root.addElement(settings);

        GeneratedXmlFile gxf1 = new GeneratedXmlFile(document, this.properties.getProperty("fileName", "mybatis-config.xml"), this.properties.getProperty("targetPackage"), this.properties.getProperty("targetProject"), false, this.context.getXmlFormatter());
        ArrayList answer1 = new ArrayList(1);
        answer1.add(gxf1);
        return answer1;
    }

    @Override
    public boolean sqlMapGenerated(GeneratedXmlFile sqlMap, IntrospectedTable introspectedTable) {
        StringBuilder sb = new StringBuilder();
        sb.append(sqlMap.getTargetPackage());
        sb.append('.');
        String temp = sb.toString();
        sb.setLength(0);
        sb.append(temp.replace('.', '/'));
        sb.append(sqlMap.getFileName());
        this.mapperFiles.add(sb.toString());
        return true;
    }
}
